package com.home.cop.sysinfo.service.impl;

import com.home.cop.employee.mapper.SysMenuMapper;
import com.home.cop.employee.model.SysMenu;
import com.home.cop.sysinfo.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper menuMapper;


    @Override
    public List<SysMenu> getSysMenuByRoleId(Integer roleId) {
        return menuToTree(menuMapper.getSysMenuByRoleId(roleId));
    }

    @Override
    public List<SysMenu> getAllSysMenu() {
        return menuToTree(menuMapper.getAllSysMenu());
    }

    /**
     * 菜单组装
     * @param list
     * @return
     */
    @Override
    public List<SysMenu> menuToTree(List<SysMenu> list){
        List<SysMenu> mainMenu = new ArrayList<>();
        for (SysMenu menu:list){
            if (menu.getSysMenuId()<100){
                menu.setChildren(new ArrayList<SysMenu>());
                mainMenu.add(menu);
            }
        }
       for(SysMenu menu:mainMenu){
            for (SysMenu menu1:list){
                if (menu1.getSysMenuId()>100 && menu.getSysMenuId() == Math.floor(menu1.getSysMenuId()/100)){
                    menu.getChildren().add(menu1);
                }
            }
           Collections.sort(menu.getChildren());
       }
       Collections.sort(mainMenu);
        return mainMenu;
    }
}
